import socket # Import socket module s = socket.socket() # Create a socket object host = socket.gethostname() # Get local machine name port = 50123 # Reserve a port for your service. s.bind((host, port)) # Bind to the port s.listen(5) # Now wait for client connection. while True: c, addr = s.accept() # Establish connection with client. print ('Got connection from ', addr) c.send(bytes('Thank you for connecting', 'UTF-8')) char = (c.recv(1024).decode('UTF-8')) while not char == 'q': if(char == '\r'): print('') elif len(char) > 1 and char == '[D': print('LEFT') elif len(char) > 1 and char.find('[C') >= 0: print('RIGHT') elif len(char) > 1 and char == '[A': print('UP') elif len(char) > 1 and char == '[B': print('DOWN') else: print(char, end="", flush=True) char = (c.recv(1024).decode('UTF-8')) c.close() # Close the connection